package mainMenu;

import java.util.ArrayList;

import utility.Sprite;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import coganh.Activity.InGame;
import coganh.Activity.R;

public class MainMenuThread extends Thread {
	public static final int MENU_SETTING = 1;
	public static final int MENU_START = 2;
	public static final int MENU_HELP = 3;
	public static final int MENU_ABOUT = 4;

	private int menu;

	private Bitmap b, temp;

	private ArrayList<MainMenuSprite> mSprites;
	private ArrayList<MainMenuSprite> mSpritestoRemove;
	private ArrayList<MainMenuSprite> mSpritesInvisible;
	private ArrayList<MainMenuSprite> mSpritestoVisible;
	private MainMenuSprite sprbtStart, sprbtSetting, sprbtHelp, sprbtAbout,
			onclick;

	private long mLastTime;

	private boolean mRun = false;

	private SurfaceHolder mSurfaceHolder;

	private Resources res;
	private MainMenuView mainMenuView;
	private boolean ischangeActivity = false;
	private boolean acceptChange = false;

	public MainMenuThread(SurfaceHolder surfaceHolder, Context context,
			MainMenuView mainMenuView) {
		mSurfaceHolder = surfaceHolder;
		res = context.getResources();

		mSprites = new ArrayList<MainMenuSprite>();
		mSpritestoRemove = new ArrayList<MainMenuSprite>();
		mSpritesInvisible = new ArrayList<MainMenuSprite>();
		mSpritestoVisible = new ArrayList<MainMenuSprite>();
		this.mainMenuView = mainMenuView;

		Initbutton();
	}

	private void Initbutton() {
		sprbtAbout = new MainMenuSprite();
		sprbtAbout.setOriented(Sprite.ORIENTED_VERTICAL);

		sprbtHelp = new MainMenuSprite();
		sprbtHelp.setOriented(Sprite.ORIENTED_VERTICAL);

		sprbtSetting = new MainMenuSprite();
		sprbtSetting.setOriented(Sprite.ORIENTED_VERTICAL);

		sprbtStart = new MainMenuSprite();
		sprbtStart.setOriented(Sprite.ORIENTED_VERTICAL);
		if (b == null)
			b = BitmapFactory.decodeResource(this.res, R.drawable.button);

		sprbtAbout.Initialize(b, 50, 300, 5, 6, true);
		sprbtHelp.Initialize(b, 50, 300, 5, 6, true);
		sprbtSetting.Initialize(b, 50, 300, 5, 6, true);
		sprbtStart.Initialize(b, 50, 300, 5, 6, true);

		sprbtAbout.setxPost(240);
		sprbtAbout.setyPost(200);
		sprbtAbout.setLabel(MENU_ABOUT);

		sprbtHelp.setxPost(240);
		sprbtHelp.setyPost(260);
		sprbtHelp.setLabel(MENU_HELP);

		sprbtSetting.setxPost(240);
		sprbtSetting.setyPost(320);
		sprbtSetting.setLabel(MENU_SETTING);

		sprbtStart.setxPost(240);
		sprbtStart.setyPost(380);
		sprbtStart.setLabel(MENU_START);

		mSprites.add(sprbtAbout);
		mSprites.add(sprbtHelp);
		mSprites.add(sprbtSetting);
		mSprites.add(sprbtStart);
	}

	@Override
	public void run() {
		while (mRun) {
			Canvas c = null;
			try {
				c = mSurfaceHolder.lockCanvas(null);
				synchronized (mSurfaceHolder) {
					doDraw(c);
					updatePhysics();
					if (ischangeActivity && acceptChange) {
						this.setRunning(false);
						changeActivity();
					}
				}
			} catch (Exception e) {
				System.out.println(e.getStackTrace());
			} finally {
				if (c != null) {
					mSurfaceHolder.unlockCanvasAndPost(c);
				}
			}
		}
	}

	/**
	 * Draws to the provided Canvas.
	 */
	private void doDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);

		for (MainMenuSprite a : mSprites) {
			a.draw(canvas);
		}

		canvas.restore();
	}

	private void updatePhysics() {
		long now = System.currentTimeMillis();

		if (mLastTime > now)
			return;
		synchronized (mSprites) {
			for (MainMenuSprite a : mSprites) {
				a.Update(now);
				if (a.isDispose()) {
					mSpritestoRemove.add(a);
					for (MainMenuSprite b : mSpritesInvisible) {
						if (a.getxPost() == b.getxPost()
								&& a.getyPost() == b.getyPost()) {
							b.setInvisible(false);
							mSpritestoVisible.add(b);
							mSpritesInvisible.remove(mSpritesInvisible
									.indexOf(b));
							// xu ly chuyen cac chuc nang phu hop voi mainmenu
							this.acceptChange = true;
						}
					}
				}
				if (a.isInvisible()) {
					mSpritesInvisible.add(a);
				}
			}
		}

		synchronized (mSpritestoRemove) {
			mSprites.removeAll(mSpritestoRemove);
			mSpritestoRemove.clear();
		}

		synchronized (mSpritesInvisible) {
			mSprites.removeAll(mSpritesInvisible);
		}
		synchronized (mSpritestoVisible) {
			mSprites.addAll(mSpritestoVisible);
			mSpritestoVisible.clear();
		}
		mLastTime = now;

	}

	public void doTouch(MotionEvent event) {

		int action = event.getAction();
		float x = event.getX(); // or getRawX();
		float y = event.getY();

		switch (action) {// cho nay can sua lai, dung utility de xu ly
		case MotionEvent.ACTION_DOWN:
			for (MainMenuSprite a : mSprites) {
				if ((x >= a.getxPost())
						&& (x <= (a.getxPost() + a.getFrameWidth()))
						&& (y >= a.getyPost())
						&& (y <= (a.getyPost() + a.getFrameHeight()))) {

					switch (a.getLabel()) {
					case MENU_ABOUT:
						this.menu = MENU_ABOUT;
						break;
					case MENU_HELP:
						this.menu = MENU_HELP;
						break;
					case MENU_SETTING:
						this.menu = MENU_SETTING;
						break;
					case MENU_START:
						this.menu = MENU_START;
						break;
					default:
						break;
					}

					this.itemSelect(a);
					mSprites.get(mSprites.indexOf(a)).setInvisible(true);
					this.ischangeActivity = true;
					break;
				}
			}
			Log.d("le duc hien|", "ve lai|:");
			break;
		}
	}

	public void setRunning(boolean b) {
		mRun = b;
	}

	public void setSurfaceSize(int width, int height) {
		synchronized (mSurfaceHolder) {

		}
	}

	private void changeActivity() {
		Intent mIntent = new Intent();
		switch (this.menu) {
		case MENU_ABOUT:
			break;
		case MENU_HELP:
			break;
		case MENU_SETTING:
			break;
		case MENU_START:
			mIntent.setClass(mainMenuView.getContext(), InGame.class);
			break;
		default:
			break;
		}
		mainMenuView.getContext().startActivity(mIntent);

	}

	private void itemSelect(MainMenuSprite sprite) {
		synchronized (mSprites) {
			temp = BitmapFactory.decodeResource(res, R.drawable.button_select);
			onclick = new MainMenuSprite();
<<<<<<< .mine
			onclick.setOriented(Sprite.ORIENTED_VERTICAL);
			onclick.setDelay(100);
=======
			onclick.setOriented(Sprite.ORIENTED_VERTICAL);
			onclick.setDelay(1000);
>>>>>>> .r17
			onclick.Initialize(temp, 50, 300, 5, 6, false);
			onclick.setxPost(sprite.getxPost() + sprite.getFrameWidth() / 2);
			onclick.setyPost(sprite.getyPost() + sprite.getFrameHeight() / 2);
			Log.d("le duc hien", "them hinh anh select");
			mSprites.add(onclick);
		}
	}
}
